package org.qufenqi.api.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.qufenqi.api.common.CommonResponse;
import org.qufenqi.api.entity.Account;
import org.qufenqi.api.entity.Cart;
import org.qufenqi.api.entity.Order;
import org.qufenqi.api.mapper.CartMapper;
import org.qufenqi.api.mapper.OrderMapper;
import org.qufenqi.api.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Service("cartService")
public class CartServiceImpl implements CartService {

    @Autowired
    private CartMapper cartMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;


    //进入购物车
    @Override
    public CommonResponse<List<Order>> getCart(String userid){

        //redis字符串序列化器
        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);

        //查询缓存
        String jsonList = JSON.toJSONString(redisTemplate.opsForValue().get(userid + "Cart"));
        List<Order> userCartInfo = (List<Order>) JSONObject.parseArray(jsonList, Order.class);

        if(userCartInfo == null) {
            System.out.println("从数据库中获取数据");
            //缓存为空，查询数据库
            QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("*").eq("userid", userid);
            userCartInfo = orderMapper.selectList(queryWrapper);

            //把获得的数据放入redis
            redisTemplate.opsForValue().set(userid+"Cart",userCartInfo,1L, TimeUnit.DAYS);
        }
        else {
            System.out.println("从缓存获取数据");
        }

        return CommonResponse.responseForSuccess(userCartInfo);
    }


    //查看订单
    @Override
    public CommonResponse<Order> getOrder(String orderid){

        //redis字符串序列化器
        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);

        //查询缓存
        String jsonList = JSON.toJSONString(redisTemplate.opsForValue().get(orderid + "Order"));
        Order orderInfo = JSON.parseObject(jsonList,Order.class);

        if(orderInfo == null) {
            System.out.println("从数据库获取数据");
            //缓存为空，查询数据库
            QueryWrapper<Order> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("*").eq("orderid", orderid);
            orderInfo = orderMapper.selectOne(queryWrapper);

            //获取数据放入redis
            redisTemplate.opsForValue().set(orderid+"Order",orderInfo,1L,TimeUnit.DAYS);
        }
        else {
            System.out.println("从缓存获取数据");
        }

        return CommonResponse.responseForSuccess(orderInfo);
    }


    //取消订单
    public CommonResponse<String> cancelOrder(String orderid){
        //redis序列化
        RedisSerializer redisSerializer = new StringRedisSerializer();
        redisTemplate.setKeySerializer(redisSerializer);

        orderMapper.deleteById(orderid);
        try {
            redisTemplate.delete(orderid+"Order");
        }
        catch (Exception ex){
            System.out.println("无缓存，不需要删除");
        }




        if(orderMapper.selectById(orderid)!=null){
            return CommonResponse.responseForErrorMessage("删除失败");
        }

        return CommonResponse.responseForSuccessMessage("成功删除");
    }
}
